package test.gateway.b.controllers;

import com.sanri.dtos.IdcardQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import test.gateway.b.dto.PrivateSomeVo;
import test.gateway.b.remote.IdcardService;
import test.gateway.b.remote.QueryTokenCommand;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.util.Date;

@RestController
@Slf4j
public class SomeController {

    @Autowired
    private IdcardService idcardService;

    @GetMapping("/some")
    public PrivateSomeVo some(String echo, HttpServletRequest request){
        log.info("serviceB 也打印点日志 [{}]",echo);
        String id = request.getSession().getId();
        PrivateSomeVo build = PrivateSomeVo.builder().sessionId(id).remoteIp(request.getRemoteUser()).now(Instant.now() + "").build();
        String echoReturn = idcardService.echo(echo);
        build.setEcho(echoReturn);
        return build;
    }

    @GetMapping("/idcard")
    public String idcard(String name,int age){
        log.info("name:{},age:{}",name,age);
        return idcardService.idcard(IdcardQueryParam.builder().name(name).age(age).build());
    }

    @GetMapping("/idcard/echo")
    public String idcardEcho(String name,int age){
        return idcardService.idcardEcho(IdcardQueryParam.builder().name(name).age(age).build(),name);
    }

    @GetMapping("/token")
    public String token(){
        log.info("测试 traceId 和 spanId ");
        return idcardService.token();
    }

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /**
     * ribbon 测试
     */
    @GetMapping("/choseServer")
    public void choseServer(){
        int _5 = 0,_6 = 0,_7 = 0;
        for (int i = 0; i < 9999; i++) {
            ServiceInstance servicea = loadBalancerClient.choose("servicea");
            int port = servicea.getPort();
            if(port == 8085){_5++;}else if(port == 8086){_6++;}else{_7++;}
        }
        System.out.println(_5+":"+_6+":"+_7);

    }

    /**
     * hystrix 测试
     * @return
     */
    @GetMapping("/queryToken")
    public String queryToken(){
        QueryTokenCommand queryTokenCommand = new QueryTokenCommand(idcardService);
        String execute = queryTokenCommand.execute();
        return execute;
    }

    @GetMapping("/reciveDate")
    public String reciveDate(){
        Date now = new Date();
        Date begin = DateUtils.addDays(now, -2);
        Date end = DateUtils.addDays(now, 1);
        String s = idcardService.reciveDate(begin, end);
        log.info("入参[{},{}],出参[{}]", DateFormatUtils.ISO_DATETIME_FORMAT.format(begin),DateFormatUtils.ISO_DATETIME_FORMAT.format(end),s);
        return s;
    }

    @GetMapping("/cookieValue")
    public String testCookieValue(@RequestHeader("token") String token){
        log.info("B 服务收到的 token 为 {}",token);
        String aserverToken = idcardService.cookieValue();
        return aserverToken;
    }
}
